<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    //搜索的方法
    public function search(){
        $keyword =  I('get.keyword','');
        if(!empty($keyword)){

            $spliterWords = FC($keyword);

            if(empty($spliterWords)){
                $spliterWords[] = $keyword;
            }
            $sql = "select * from question where ";
            //调用方法 拆分搜索的关键字 
            
            foreach ($spliterWords as $key => $value) {
                if( count($spliterWords) - 1  == $key ){
                    $sql.=" title like '%{$value}%'";
                }else{
                    $sql.=" title like '%{$value}%' or";
                }
                
            }
            $res = M()->query($sql);
            foreach ($res as $key => &$value) {
                foreach ($spliterWords as $k => $v) {
                    $value['title']=str_replace($v, "<span style='color:red'>{$v}</span>", $value['title']);
                    $value['content']=str_replace($v, "<span style='color:red'>{$v}</span>", $value['content']);
                }
            }
            $res['count'] = count($res); //为$res数组追加多一条有多少条问题
            $this->assign('searchRes',$res);
            $this->display();
        }else{
            $this->error('非法请求');
        }
    }
	
    public function index(){

        $this->display();

    }

    //异步的数据的方法 调用下面三个的方法
    public function GetThisData(){
    	$actionName = array_values($_POST)[0];   //获得推过来的值
    	$this->ajaxReturn($this->$actionName());   	//调用对应的方法
    }

    //得到最新的数据
    public function GetBestNew(){
    	$table = M('question');  //实例化模型
    	// 最新数据
        $count      = $table->where('create_time > 1467077592')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(2)
        $show       = $Page->show();// 分页显示输出
    	$data = $table->where('create_time > 1467077592')->order('create_time desc')->select();//
    	$str = '';
        foreach ($data as $key => $value) {
            
            //得到对应的标签名字
            $cateTagSql = "select t1.cname from category t1 RIGHT  JOIN category_question t2 on t1.id = t2.cid LEFT JOIN question t3 ON t3.id = t2.qid WHERE t2.qid = {$value['id']}";
            // var_dump($cateTagSql);
            $Tagresult = M()->query($cateTagSql); 
            //如果有回答 就要得到最新回答人的名字
            if($value['answer_num'] != 0){
                //回到回答者的名字的Sql语句
                $sql = "select t3.username,t3.id from question t1 LEFT JOIN answer t2 on t1.id = t2.qid LEFT JOIN user t3 on t2.uid=t3.id where t1.id = {$value['id']} order by t2.create_time desc limit 1";
                $result = M()->query($sql);

                $value['username'] = $result[0]['username'];
                $value['userid'] = $result[0]['id'];

                $str.=  '<section class="stream-list__item">';
                $str.=  '<div class="qa-rank">
                           <div class="votes hidden-xs">'.$value["vote_num"].'<small>投票</small></div>
                           <div class="answers answered">'.$value["answer_num"].'<small>回答</small></div>
                           <div class="views hidden-xs">'.$value["view"].'<small>浏览</small></div>
                        </div>';
                //判断是否已经回答 已经回答显示回答的人的名称 否则显示提问者的名称        
                $str.=  '<div class="summary">

                            <ul class="author list-inline">
                                <li>
                                    <a href="'.U("home/user/userhome",array('uid'=>$value['userid'])).'">'.$value['username'].'</a>
                                    <span class="split"></span>
                                    <a href="/q/1010000005766646/a-1020000005767065">回答</a>
                                </li>
                            </ul>
                            <h2 class="title">
                                <a href="'.U("home/Blogs/bloginfo",array('qid'=>$value["id"])).'">'.$value["title"].'</a>
                            </h2>
                            <ul class="taglist--inline ib">';

                foreach ($Tagresult as $key => $value) {
                    $str.='<li class="tagPopup"><a class="tag tag-sm" href="/t/mysql%E7%B4%A2%E5%BC%95" data-toggle="popover" data-original-title="'.$value['cname'].'" data-id="1040000003781861">'.$value['cname'].'</a></li>';
                }
                
                $str.=   '</ul>
                        </div>';
                $str.=  '</section>';
            }
            else{
                //显示提问者的名字
                $sql = "select user.username,user.id from user,question where user.id = question.uid order by create_time desc limit 1";
                $result = M()->query($sql);
                $value['username'] = $result[0]['username'];
                $value['userid'] = $result[0]['id'];

                $str.=  '<section class="stream-list__item">';
                $str.=  '<div class="qa-rank">
                           <div class="votes hidden-xs">'.$value["vote_num"].'<small>投票</small></div>
                           <div class="answers">'.$value["answer_num"].'<small>回答</small></div>
                           <div class="views hidden-xs">'.$value["view"].'<small>浏览</small></div>
                        </div>';
                //判断是否已经回答 已经回答显示回答的人的名称 否则显示提问者的名称        
                $str.=  '<div class="summary">
                            <ul class="author list-inline">
                                <li>
                                    <a href="'.U("home/user/userhome",array('uid'=>$value['userid'])).'">'.$value['username'].'</a>
                                    <span class="split"></span>
                                    <a href="/q/1010000005766646/a-1020000005767065">提问</a>
                                </li>
                            </ul>
                            <h2 class="title">
                                <a href="'.U("home/Blogs/bloginfo",array('qid'=>$value["id"])).'">'.$value["title"].'</a>
                            </h2>
                            <ul class="taglist--inline ib">';
                foreach ($Tagresult as $k => $v) {
                    $str.='<li class="tagPopup"><a class="tag tag-sm" href="/t/mysql%E7%B4%A2%E5%BC%95" data-toggle="popover" data-original-title="'.$v['cname'].'" data-id="1040000003781861">'.$v['cname'].'</a></li>';
                }

                               
                $str.=   '</ul>
                        </div>';
                $str.=  '</section>';
            }
        }
        $returnMsg = array(
                'error'  => 0,
                'texts'  => $str,
                'page'   => $show,
            );

        return $returnMsg;
    }

    //得到最热门的数据
    public function GetBestHot(){
    	// var_dump('I am GetBestHot');
    	$table = M('question');  //实例化模型
    	//获得数据
        $count      = $table->where('view > 400')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(2)
        $show       = $Page->show();// 分页显示输出
    	$data = $table->where('view > 400')->order('create_time desc')->select();
    	$str = '';
    	foreach ($data as $key => $value) {
    		
    		//得到对应的标签名字
    		$cateTagSql = "select t1.cname from category t1 RIGHT  JOIN category_question t2 on t1.id = t2.cid LEFT JOIN question t3 ON t3.id = t2.qid WHERE t2.qid = {$value['id']}";
            // var_dump($cateTagSql);
    		$Tagresult = M()->query($cateTagSql); 
    		//如果有回答 就要得到最新回答人的名字
    		if($value['answer_num'] != 0){
    			//回到回答者的名字的Sql语句
    			$sql = "select t3.username,t3.id from question t1 LEFT JOIN answer t2 on t1.id = t2.qid LEFT JOIN user t3 on t2.uid=t3.id where t1.id = {$value['id']} order by t2.create_time desc limit 1";
    			$result = M()->query($sql);
    			$value['username'] = $result[0]['username'];
                $value['userid'] = $result[0]['id'];

				$str.=  '<section class="stream-list__item">';
				$str.=  '<div class="qa-rank">
		                   <div class="votes hidden-xs">'.$value["vote_num"].'<small>投票</small></div>
		                   <div class="answers answered">'.$value["answer_num"].'<small>回答</small></div>
		                   <div class="views hidden-xs">'.$value["view"].'<small>浏览</small></div>
		                </div>';
		        //判断是否已经回答 已经回答显示回答的人的名称 否则显示提问者的名称        
		        $str.=  '<div class="summary">

		                    <ul class="author list-inline">
		                        <li>
		                            <a href="'.U("home/user/userhome",array('uid'=>$value['userid'])).'">'.$value['username'].'</a>
		                            <span class="split"></span>
		                            <a href="/q/1010000005766646/a-1020000005767065">回答</a>
		                        </li>
		                    </ul>
		                    <h2 class="title">
		                        <a href="'.U("home/Blogs/bloginfo",array('qid'=>$value["id"])).'">'.$value["title"].'</a>
		                    </h2>
		                    <ul class="taglist--inline ib">';

                foreach ($Tagresult as $key => $value) {
                    $str.='<li class="tagPopup"><a class="tag tag-sm" href="/t/mysql%E7%B4%A2%E5%BC%95" data-toggle="popover" data-original-title="'.$value['cname'].'" data-id="1040000003781861">'.$value['cname'].'</a></li>';
                }
                
                $str.=   '</ul>
		                </div>';
                $str.=	'</section>';
            }else{
            	$sql = "select user.username,user.id from user,question where user.id = question.uid order by create_time desc limit 1";
            	$result = M()->query($sql);
    			$value['username'] = $result[0]['username'];
                $value['userid'] = $result[0]['id'];

            	$str.=  '<section class="stream-list__item">';
				$str.=  '<div class="qa-rank">
		                   <div class="votes hidden-xs">'.$value["vote_num"].'<small>投票</small></div>
		                   <div class="answers">'.$value["answer_num"].'<small>回答</small></div>
		                   <div class="views hidden-xs">'.$value["view"].'<small>浏览</small></div>
		                </div>';
		        //判断是否已经回答 已经回答显示回答的人的名称 否则显示提问者的名称        
		        $str.=  '<div class="summary">
		                    <ul class="author list-inline">
		                        <li>
		                            <a href="'.U("home/user/userhome",array('uid'=>$value['userid'])).'">'.$value['username'].'</a>
		                            <span class="split"></span>
		                            <a href="/q/1010000005766646/a-1020000005767065">提问</a>
		                        </li>
		                    </ul>
		                    <h2 class="title">
		                        <a href="'.U("home/Blogs/bloginfo",array('qid'=>$value["id"])).'">'.$value["title"].'</a>
		                    </h2>
		                    <ul class="taglist--inline ib">';
                foreach ($Tagresult as $k => $v) {
                    $str.='<li class="tagPopup"><a class="tag tag-sm" href="/t/mysql%E7%B4%A2%E5%BC%95" data-toggle="popover" data-original-title="'.$v['cname'].'" data-id="1040000003781861">'.$v['cname'].'</a></li>';
                }

		                       
                $str.=   '</ul>
                        </div>';
                $str.=  '</section>';
            }
    	}

    	$returnMsg = array(
    			'error'  => 0,
    			'texts'  => $str,
                'page'   => $show,
    		);
    	return $returnMsg;
    	
    }

    //得到未回答的
   	public function GetUnanswer(){
        //页
   		$table = M('question');  //实例化模型
        //获得数据
        $count      = $table->where('answer_num = 0')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(2)
        $show       = $Page->show();// 分页显示输出
        $data = $table->where('answer_num = 0 ')->order('create_time desc')->select();
        $str = '';
        foreach ($data as $key => $value) {
            
            //得到对应的标签名字
            $cateTagSql = "select t1.cname from category t1 RIGHT  JOIN category_question t2 on t1.id = t2.cid LEFT JOIN question t3 ON t3.id = t2.qid WHERE t2.qid = {$value['id']}";
            $Tagresult = M()->query($cateTagSql);

            // 得到提问者的名字
            $sql = "select user.username,user.id from user,question where user.id = question.uid order by create_time desc limit 1";
            $result = M()->query($sql);
            $value['username'] = $result[0]['username'];
            $value['userid'] = $result[0]['id'];


            $str.=  '<section class="stream-list__item">';
            $str.=  '<div class="qa-rank">
                       <div class="votes hidden-xs">'.$value["vote_num"].'<small>投票</small></div>
                       <div class="answers">'.$value["answer_num"].'<small>回答</small></div>
                       <div class="views hidden-xs">'.$value["view"].'<small>浏览</small></div>
                    </div>';
            //判断是否已经回答 已经回答显示回答的人的名称 否则显示提问者的名称        
            $str.=  '<div class="summary">
                        <ul class="author list-inline">
                            <li>
                                <a href="'.U("home/user/userhome",array('uid'=>$value['userid'])).'">'.$value['username'].'</a>
                                <span class="split"></span>
                                <a href="/q/1010000005766646/a-1020000005767065">提问</a>
                            </li>
                        </ul>
                        <h2 class="title">
                            <a href="'.U("home/Blogs/bloginfo",array('qid'=>$value["id"])).'">'.$value["title"].'</a>
                        </h2>
                        <ul class="taglist--inline ib">';
            foreach ($Tagresult as $k => $v) {
                $str.='<li class="tagPopup"><a class="tag tag-sm" href="/t/mysql%E7%B4%A2%E5%BC%95" data-toggle="popover" data-original-title="'.$v['cname'].'" data-id="1040000003781861">'.$v['cname'].'</a></li>';
            }

                           
            $str.=   '</ul>
                    </div>';
            $str.=  '</section>';
        }
        $returnMsg = array(
                'error'  => 0,
                'texts'  => $str,
                'page'   => $show,
            );
        return $returnMsg;
   	}
}

?>